package cn.tedu.eshihui.service.impl;

import cn.tedu.eshihui.ex.ServiceException;
import cn.tedu.eshihui.mapper.*;
import cn.tedu.eshihui.pojo.dto.BusinessAddNewDTO;
import cn.tedu.eshihui.pojo.dto.BusinessUpdateDTO;
import cn.tedu.eshihui.pojo.entity.Business;
import cn.tedu.eshihui.pojo.vo.BusinessListItemVO;
import cn.tedu.eshihui.pojo.vo.BusinessStandardVO;
import cn.tedu.eshihui.service.IBusinessService;
import cn.tedu.eshihui.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BusinessServiceImpl implements IBusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RecommendMapper recommendMapper;
    @Autowired
    private ShoppingMapper shoppingMapper;


    @Override
    public void addNew(BusinessAddNewDTO businessAddNewDTO) {
       int i= businessMapper.countByName(businessAddNewDTO.getStore());
       if (i>0){
           String message="添加时出现异常，店铺名称已占用";
           log.warn("添加时出现异常，店铺名册已占用");
           throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
       }
       Business business=new Business();
       BeanUtils.copyProperties(businessAddNewDTO,business);
       int rows=businessMapper.insert(business);
        if (rows != 1) {
            String message = "添加店铺失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }


    }

    @Override
    public void delete(Long id) {
        {
            BusinessStandardVO business = businessMapper.getStandardById(id);
            if (business == null) {
                String message = "删除店铺失败，尝试删除的数据不存在";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }
        {
            int count = productMapper.countByBusiness(id);
            if (count > 0) {
                String message = "删除店铺失败，尝试访问的数据存在关联的product数据";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        {
            int count = collectMapper.countByBusiness(id);
            if (count > 0) {
                String message = "删除店铺失败，尝试访问的数据存在关联的collect数据";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        {
            int count = recommendMapper.countByBusiness(id);
            if (count > 0) {
                String message = "删除店铺失败，尝试访问的数据存在关联的recommend数据";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        {
            int count = shoppingMapper.countByBusiness(id);
            if (count > 0) {
                String message = "删除店铺失败，尝试访问的数据存在关联的shopping数据";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        int rows = businessMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除店铺失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id,BusinessUpdateDTO businessUpdateDTO) {
        {
            BusinessStandardVO album = businessMapper.getStandardById(id);
            if (album == null) {
                String message = "修改详情失败，尝试访问的数据不存在";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
            }
        }
        {
            int count= businessMapper.countByNameAndNotId(id,businessUpdateDTO.getStore());
            if (count>0){
                String message="修改店铺详情失败，新的店铺名称已经被占用";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
            }
        }
        Business album=new Business();
        BeanUtils.copyProperties(businessUpdateDTO,album);
        album.setId(id);
        log.debug("即将执行修改数据，参数{}",album);
        int rows = businessMapper.update(album);
        if (rows != 1) {
            String message = "修改店铺详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public BusinessStandardVO getStandardById(Long id) {
       BusinessStandardVO business=businessMapper.getStandardById(id);
       if (business==null){
           String message = "查询店铺详情失败，尝试访问的数据不存在";
           log.warn(message);
           throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
       }
       return business;
    }

    @Override
    public List<BusinessListItemVO> list() {
        List<BusinessListItemVO>list=businessMapper.list();
        return list;
    }
}
